/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.mvc.helpers
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	internal class DelayContext extends Context
	{
		private static var _pool:Array = new Array();
		private var _delay:int;
		private var _timer:Timer;
		private var _start:int;
		private var _elapsed:int;
		public function DelayContext() {
		}
				
		public static function getInstance(delay:int,init:Function,fnc:Function):Context {
			var context:DelayContext;
			if (_pool.length == 0) {
				context = new DelayContext();
			} else {
				context = _pool.shift();
			}
			context.initialize(delay,init,fnc);
			return context;
		}
		
		private function initialize(delay:int,init:Function,fnc:Function):void {
			_delay = delay;
			setup(init, fnc);
			if (fnc == null) {
				throw new ArgumentError("The third argument must NOT be null.");
			}
		}
		
		public static function debug():void {
			for each(var handler:Context in handlers) {
				if (handler is DelayContext) {
//					trace((handler as DelayHandler)._owner,(handler as DelayHandler)._owner.name);
					trace((handler as DelayContext).after());
				}
			}
		}
				
		override public function start():void {
			_elapsed = 0;
			addEvent(_delay);
		}
		
		override public function resume():void {
			addEvent(_delay-_elapsed);			
		}
		
		override public function stop():void {
			if (_timer != null) {
				_timer.removeEventListener(TimerEvent.TIMER, timerHandler);
			}
			_timer = null;
			_elapsed += (getTimer() - _start);
			//cleanUp();
		}
		
		override protected function cleanUp():void {
			super.cleanUp();
			if (_timer != null) {
				_timer.removeEventListener(TimerEvent.TIMER, timerHandler);
			}
			_pool.push(this);
		}
		
		private function addEvent(delay:int):void {		
			if (delay >= 0) {
				if (_timer != null) {
					_timer.removeEventListener(TimerEvent.TIMER, timerHandler);
				}
				_timer = new Timer(delay, 1);
				_timer.addEventListener(TimerEvent.TIMER, timerHandler);
				_timer.start();
				_start = getTimer();
			}
		}
		
		private function timerHandler(event:TimerEvent):void {
			_timer.removeEventListener(TimerEvent.TIMER, timerHandler);
			_timer = null;
			wrapUp();
			dispatch(DONE);
		}
		
		public static function clear():void {
			while(_pool.length > 0) {
				_pool.pop();
			}
		}
	}
}